package crp.core.security.domain.repositories;

import com.qwlabs.lang.C2;
import crp.core.user.domain.models.UserEntity;
import crp.core.security.domain.models.UserRoleEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;

import javax.enterprise.context.ApplicationScoped;
import java.util.Collection;
import java.util.stream.Stream;

@ApplicationScoped
public class UserRoleRepository implements PanacheRepositoryBase<UserRoleEntity, String> {

    public Stream<UserRoleEntity> findByUserIds(Collection<String> userIds) {
        if (C2.isEmpty(userIds)) {
            return Stream.empty();
        }
        return find("user.id in :userIds", Parameters.with("userIds", userIds))
                .stream();
    }

    public long deleteByUser(UserEntity user) {
        return delete("user = :user", Parameters.with("user", user));
    }

    public long deleteByRoleId(String roleId) {
        return delete("role.id=:roleId", Parameters.with("roleId", roleId));
    }
}
